//
//  DCProgressTool.m
//  Driver
//
//  Created by 大橙子 on 2018/11/8.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCProgressTool.h"
@interface DCProgressTool ()
@property(nonatomic,copy)sureBtnBlock sureBlock;
@property(nonatomic,copy)CancleBtnBlock cancleBlock;

@property (nonatomic,strong) DCImageAndTextLabel *startLocationLabel;
@property (nonatomic,strong) DCImageAndTextLabel *endLocationLabel;
@property (nonatomic,strong) DCImageAndTextField *priceLabel;
@end
@implementation DCProgressTool
+(instancetype)sharedHUDTool
{
    static DCProgressTool *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[DCProgressTool alloc]init];
    });
    return _manager;
}
-(UIView *)showProgressTitle:(NSString *)title model:(DCQuotationModel *__nullable)model type:(NSInteger)type sureBlock:(sureBtnBlock)sureBlock cancleBlock:(CancleBtnBlock)cancleBlock
{
    __weak typeof(self) weakself = self;
    self.sureBlock = sureBlock;
    self.cancleBlock = cancleBlock;
    
    CGFloat kBaseHeight = 40;
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];
    backView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    backView.userInteractionEnabled = YES;
    
    UIView *contentView = [[UIView alloc]init];
    contentView.width = 0.8*backView.width;
    contentView.height =kBaseHeight*6+20;
    contentView.center = CGPointMake(backView.centerX, backView.centerY);
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.userInteractionEnabled = YES;
    contentView.layer.cornerRadius = 10;
    [backView addSubview:contentView];
    
    UILabel *titlelLabel = [[UILabel alloc]init];
    titlelLabel.text = title;
    titlelLabel.textAlignment = NSTextAlignmentCenter;
    titlelLabel.font = [UIFont systemFontOfSize:20];
    titlelLabel.textColor = [UIColor blackColor];
    titlelLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titlelLabel];
    [titlelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.left.equalTo(contentView);
        make.right.equalTo(contentView);
        make.height.mas_offset(kBaseHeight);
    }];
    
    CGFloat line = 0.5;
    UILabel *line1 = [[UILabel alloc]init];
    line1.backgroundColor = [UIColor lightGrayColor];
    line1.alpha = 0.8;
    [contentView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titlelLabel.mas_bottom);
        make.left.equalTo(contentView);
        make.right.equalTo(contentView);
        make.height.mas_offset(line);
    }];
    
    self.startLocationLabel = [[DCImageAndTextLabel alloc]init];
    self.startLocationLabel.textLabel.font = FONT(15);
    self.startLocationLabel.textLabel.textColor = [UIColor blackColor];
    self.startLocationLabel.textLabel.text = model.start;
    //    textLabel.textLabel.textAlignment = NSTextAlignmentCenter;
    self.startLocationLabel.imageView.image = [UIImage imageNamed:@"起点"];
    [contentView addSubview:self.startLocationLabel];
    [self.startLocationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom);
        make.left.equalTo(contentView.mas_left).offset(10);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(kBaseHeight);
    }];
    
    UILabel *line2 = [[UILabel alloc]init];
    line2.backgroundColor = [UIColor lightGrayColor];
    line2.alpha = 0.8;
    [contentView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.startLocationLabel.mas_bottom).offset(3);
        make.left.equalTo(contentView.mas_left).offset(40);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(line);
    }];
    
    self.endLocationLabel = [[DCImageAndTextLabel alloc]init];
    self.endLocationLabel.textLabel.font = FONT(15);
    self.endLocationLabel.textLabel.textColor = [UIColor blackColor];
    self.endLocationLabel.textLabel.text = model.end;
    //    textLabel.textLabel.textAlignment = NSTextAlignmentCenter;
    self.endLocationLabel.imageView.image = [UIImage imageNamed:@"终点"];
    [contentView addSubview:self.endLocationLabel];
    [self.endLocationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom);
        make.left.equalTo(contentView.mas_left).offset(10);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(kBaseHeight);
    }];
    
    DCImageAndTextLabel *carTypeLabel = [[DCImageAndTextLabel alloc]init];
    carTypeLabel.textLabel.font = FONT(14);
    carTypeLabel.textLabel.textColor = [UIColor blackColor];
    carTypeLabel.textLabel.text = model.car;
//    carTypeLabel.backgroundColor = [UIColor redColor];
    carTypeLabel.imageView.image = [UIImage imageNamed:@"立方体"];
    [contentView addSubview:carTypeLabel];
    [carTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.endLocationLabel.mas_bottom);
        make.left.equalTo(contentView.mas_left).offset(30);
        make.width.mas_offset(100*WIDTHSCALE6);
        make.height.mas_offset(kBaseHeight-10);
    }];
    
    UILabel *numLabel = [[UILabel alloc]init];
    numLabel.font = FONT(15);
    numLabel.textColor = [UIColor blackColor];
    numLabel.textAlignment = NSTextAlignmentCenter;
//    numLabel.backgroundColor = [UIColor yellowColor];
    numLabel.text = [NSString stringWithFormat:@"%@辆",model.num];
    [contentView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakself.endLocationLabel.mas_bottom);
        make.left.equalTo(carTypeLabel.mas_right).offset(10);
        make.width.mas_offset(100*WIDTHSCALE6);
        make.height.mas_offset(kBaseHeight-10);
    }];
    
    DCImageAndTextField *priceLabel = [[DCImageAndTextField alloc]init];
    priceLabel.imageView.image = IMAGENAME(@"钱");
    priceLabel.textField.placeholder = @"请填写报价";
    if (type == 1) {
        priceLabel.textField.text = model.price;
    }
    self.priceLabel = priceLabel;
    [contentView addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(carTypeLabel.mas_bottom);
        make.left.equalTo(contentView.mas_left).offset(10);
        make.right.equalTo(contentView.mas_right).offset(-10);
        make.height.mas_offset(kBaseHeight);
    }];
    
    CGFloat btnWidth = (contentView.width - 100*WIDTHSCALE6)/2;
    UIButton * surtBtn = [[UIButton alloc]init];
    [surtBtn setTitle:@"提交" forState:UIControlStateNormal];
    [surtBtn setTitleColor:BlueColor forState:UIControlStateNormal];
    surtBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [surtBtn addTarget:self action:@selector(surtBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
    surtBtn.layer.cornerRadius = 18;
    surtBtn.layer.masksToBounds = YES;
    surtBtn.layer.borderWidth = 1;
    surtBtn.layer.borderColor = BlueColor.CGColor;
    [contentView addSubview:surtBtn];
    [surtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(btnWidth);
        make.height.mas_offset(35);
        make.left.equalTo(contentView.mas_left).offset(40);
        make.bottom.equalTo(contentView.mas_bottom).offset(-10);
    }];
    
    UIButton * cancleBtn = [[UIButton alloc]init];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancleBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.layer.cornerRadius = 18;
    cancleBtn.layer.masksToBounds = YES;
    cancleBtn.layer.borderWidth = 1;
    cancleBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    [contentView addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(btnWidth);
        make.height.mas_offset(35);
        make.right.equalTo(contentView.mas_right).offset(-40);
        make.bottom.equalTo(contentView.mas_bottom).offset(-10);
    }];
    
    return backView;
}

-(void)surtBtnOnClicked:(UIButton *)btn
{
    UIView *view = (UIView *)btn.superview.superview;
    [view removeFromSuperview];
    if (self.sureBlock) {
        self.sureBlock(self.priceLabel.textField.text);
    }
}

-(void)cancleBtnOnClicked:(UIButton *)btn
{
    UIView *view = (UIView *)btn.superview.superview;
    [view removeFromSuperview];
    if (self.cancleBlock) {
        self.cancleBlock(self.cancleBlock);
    }
}

@end
